﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace GameFramework.skill.selector.impl
{
    /// <summary>
    /// 扇形选择器
    /// </summary>
    public class SectorSkillSelector:ISkillSelector
    {

        public void selectTarget(Skill skill, Transform skillPrefabInstance)
        {
            skill.targets.Clear();
            skill.targetPos.Clear();
            //根据技能数据中得标签 获取所有目标
            List<Transform> taragets = new List<Transform>();
            SkillConfig skillConfig = skill.skillConfig;
            List<GameObject> tempTagObject = new List<GameObject>();
            if (skillConfig.targetType == 2)//查找目标
            {
                var targetTag = skillConfig.targetTag;
                int enimyTag = targetTag & 0x01;
                //TODO 计算出所有tag
                if (enimyTag == 1)
                {
                    tempTagObject.AddRange(GameObject.FindGameObjectsWithTag("Enimy"));
                    // for (int i = 0; i < tempTagNum; i++)
                    // {
                    //     //TODO 找出周围有这个tag的对象，九宫格算法或其他算法
                    //     tempTagObject = GameObject.FindGameObjectsWithTag(skillConfig);
                    // }

                    // tempTagObject = GameObject.FindGameObjectsWithTag("Enimy");
                    for (int i = 0; i < tempTagObject.Count; i++)
                    {
                        taragets.Add(tempTagObject[i].GetComponent<Transform>());
                    }
                    //判断攻击范围
                    taragets = taragets.FindAll(t =>
                            Vector3.Distance(t.position, skillPrefabInstance.position) <= skill.skillConfig.releaseRange 
                        // Vector3.Distance(t.position, skillPrefabInstance.position) <= skill.skillConfig.releaseRange &&
                        // Vector3.Angle(skillPrefabInstance.forward, t.position - skillPrefabInstance.position) <= skill.skillConfig.angle / 2
                        // true
                    );
                    if (taragets.Count == 0)
                    {
                        Debug.Log("没有敌人");
                    }
                    else
                    {
                        for (int i = 0; i < taragets.Count; i++)
                        {
                            Debug.Log("找到目标："+taragets[i].name);
                        }
                    }
                }
            }else if (skillConfig.targetType == 3)//查找地点
            {
                //获取鼠标点
                Ray screenPointToRay = Camera.main.ScreenPointToRay(Input.mousePosition);
                bool raycast = Physics.Raycast(screenPointToRay, out RaycastHit hitInfo);
                if (raycast)
                {
                    if (Vector3.Distance(hitInfo.point, skillPrefabInstance.position) <= skill.skillConfig.releaseRange)
                    {
                        skill.targetPos.Add(hitInfo.point);
                        Debug.Log("找到目标点："+hitInfo.point);
                    }
                }
            }
            
            
            //TODO 继续筛选目标，目标的状态释放可攻击，目标是否死亡等

        }
    }
}